﻿            var divArray;
		var imageArray = [];

            function initialGame() {
                fillArray();
		shufflePuzzles();
                $("div.puzzleCellAll").click(clickPuzzleCell);
                $("a#ShuffleMode").click(shufflePuzzles);
		$("a#changeImage").click(changePuzzleImage);
		imageArray.push("puzzleImage1.jpg");
		imageArray.push("puzzleImage2.jpg");
		imageArray.push("puzzleImage3.jpg");
		imageArray.push("puzzleImage4.jpg");
		imageArray.push("puzzleImage5.jpg");
		imageArray.push("puzzleImage6.jpg");
            }

            function fillArray() {
                divArray = Array(16);
                var divs = $("div.puzzleCellAll");

                for (var i = 0; i < divs.length; i++) {
                    divArray[i] = new puzzleObj($(divs[i]).attr("left"), $(divs[i]).attr("top"), $(divs[i]).css("background-position"), $(divs[i]).css("visibility"));
                }
            }

            function changePuzzleImage() {
		var rndPuzzleImage = Math.floor(Math.random() * imageArray.length);
		$("div.puzzleCellAll").css("background-image", 'url(Images/'+imageArray[rndPuzzleImage]+')');
		$("#PreviewImage div img").attr("src", 'Images/'+imageArray[rndPuzzleImage]);
            }

            function shufflePuzzles() {
                var divs = $("div.puzzleCellAll");
                var tempDivArray = divArray.concat();

                for (var i = 0; i < divs.length; i++) {
                    var rndPuzzleCell = Math.floor(Math.random() * tempDivArray.length);

                    $(divs[i]).attr("left", tempDivArray[rndPuzzleCell].xPosition);
                    $(divs[i]).attr("top", tempDivArray[rndPuzzleCell].xPosition);
                    $(divs[i]).css("background-position", tempDivArray[rndPuzzleCell].bgPosition);
                    $(divs[i]).css("visibility", tempDivArray[rndPuzzleCell].Visibility);

                    tempDivArray.splice(rndPuzzleCell, 1);
                }
            }

            function puzzleObj(xPos, yPos, bgPos, visible) {
                this.xPosition = xPos;
                this.yPosition = yPos;
                this.bgPosition = bgPos;
                this.Visibility = visible;
            }

            function clickPuzzleCell() {
                var ss = $("div.puzzleCellAll");

                for (var i = 0; i < ss.length; i++) {
                    if ((parseInt($(ss[i]).css("top")) == parseInt($(this).css("top")) + 160 && parseInt($(ss[i]).css("left")) == parseInt($(this).css("left"))) ||
                        (parseInt($(ss[i]).css("top")) == parseInt($(this).css("top")) - 160 && parseInt($(ss[i]).css("left")) == parseInt($(this).css("left"))) ||
                        (parseInt($(ss[i]).css("left")) == parseInt($(this).css("left")) + 160 && parseInt($(ss[i]).css("top")) == parseInt($(this).css("top"))) ||
                        parseInt($(ss[i]).css("left")) == parseInt($(this).css("left")) - 160 && parseInt($(ss[i]).css("top")) == parseInt($(this).css("top"))) {

                        if ($(ss[i]).css("visibility") == "hidden") {
                          
                            $(ss[i]).css("visibility", "visible");
                            $(this).css("visibility", "hidden");
                            var xpos = $(ss[i]).attr("left");
                            var ypos = $(ss[i]).attr("top");
                            var backPos = $(ss[i]).css("background-position");
                            $(ss[i]).attr("left", $(this).attr("left"));
                            $(ss[i]).attr("top", $(this).attr("top"));
                            $(ss[i]).css("background-position", $(this).css("background-position"));
                            $(this).attr("left", xpos);
                            $(this).attr("top", ypos);
                            $(this).css("background-position", backPos);
                            
                            break;
                        }
                    }
                }

            }